package com.yc.demo.d0303;

import com.google.gson.Gson;
import com.yc.demo.bean.Result;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.Date;

@WebServlet(name = "login2.s", value = "/login2.s")
public class Login2Servlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        Result result = new Result(0,"",null);

        // 验证用户输入的验证码是否正确
        // 获取用户输入的验证码
        String ucode = request.getParameter("ucode");
        // 获取会话中保存的验证码
        Object vcode = request.getSession().getAttribute("vcode");
        System.out.println("ucode:"+ucode);
        System.out.println("vcode:"+vcode);

        if (!vcode.equals(ucode)){
            result.setMsg("验证码输入错误！");
            System.out.println("错误");
        } else {
            String name = request.getParameter("name");
            String pwd = request.getParameter("pwd");
            if (("yc".equals(name) || "ls".equals(name) || "zs".equals(name))&& "123".equals(pwd)){
                result.setCode(1);
                result.setMsg("登录成功！");

                // 由于要显示登录事件，添加时间
                request.getSession().setAttribute("loginedTime", new Date());
                request.getSession().setAttribute("loginedUser", name);

                // 获取登录前访问的页面
                final Object requestURI = request.getSession().getAttribute("requestURI");
                // 放到结果对象中
                result.setData(requestURI);
            } else {
                result.setMsg("用户名或密码错误！");
            }
        }
        System.out.println();
        String json = new Gson().toJson(result);
        response.getWriter().append(json);
    }

/*    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
////        request.setCharacterEncoding("utf-8");
////        response.setCharacterEncoding("utf-8");
////        response.setContentType("application/json;charset=utf-8");
////
////        Result result = new Result(0,"",null);
////
////        // 验证用户输入的验证码是否正确
////        // 获取用户输入的验证码
////        String ucode = request.getParameter("ucode");
////        // 获取会话中保存的验证码
////        Object vcode = request.getSession().getAttribute("vcode");
////        if (!vcode.equals(ucode)){
////            result.setMsg("验证码输入错误！"+vcode+" "+ucode);
////
////        } else {
////            final String name = request.getParameter("name");
////            final String pwd = request.getParameter("pwd");
////            if ("yc".equals(name) && "123".equals(pwd)){
////                result.setCode(1);
////                result.setMsg("登录成功！");
////                request.getSession().setAttribute("loginedUser",name);
////            } else {
////                result.setMsg("用户名或密码错误！");
////            }
////        }
////
////        String json = new Gson().toJson(result);
////        response.getWriter().append(json);
//    }*/
}
